#include <bits/stdc++.h>
using namespace std;

const int N = 1e5 + 5;

struct BIT
{
    int c[N], n;

    void reset()
    {
        memset(c, 0, sizeof c);
    }

    void add(int p, int v)
    {
        for (int i = p; i <= n; i += i & -i)
            c[i] += v;
    }

    int qry(int x)
    {
        int ret = 0;
        for (int i = x; i; i -= i & -i)
            ret += c[i];
        return ret;
    }
} bit[20];

int n, m, t, a0[N], a[N], q[N], x[N], d[20][N], l[20][N], tmp, ans[N], _ty;

string s;

signed main()
{
    freopen("arena.in", "r", stdin);
    freopen("arena.out", "w", stdout);
    cin >> n >> m;
    for (int i = 1; i <= n; i++)
        cin >> a0[i];
    for (int i = 1; i <= m; i++)
        cin >> q[i];
    for (int i = 2, r = 1; i / 2 < n; i <<= 1, r++)
        _ty = i;
    for (int i = 2, r = 1; i / 2 < n; i <<= 1, r++)
    {
        cin >> s;
        for (int j = 0; j < s.length(); j++)
            d[r][j] = s[j] - '0';
        for (int j = 0; j < _ty; j++)
        {
            l[r][j + 1] = l[r - 1][j + 1];
            if (d[r][j / i] == 1 && j % i >= i / 2)
                l[r][j + 1] = max(l[r][j + 1], r);
            if (d[r][j / i] == 0 && j % i < i / 2)
                l[r][j + 1] = max(l[r][j + 1], r);
        }
    }
    cin >> t;
    while (t--)
    {
        memset(ans, 0, sizeof ans);
        for (int i = 0; i < 4; i++)
            cin >> x[i];
        for (int i = 1; i <= n; i++)
            a[i] = a0[i] ^ x[i % 4];
        for (int r = 0, t = 1; t / 2 < n; t <<= 1, r++)
        {
            bit[r].n = _ty;
            bit[r].reset();
            for (int x = 1; x <= t / 2; x++)
                bit[r].add(x, a[x] >= l[r][x]);
            for (int n0 = t / 2 + 1; n0 <= min(t, n); n0++)
            {
                bit[r].add(n0, a[n0] >= l[r][n0]);
                for (int j = 0; j < t; j++)
                {
                    bool flag = a[j + 1] >= l[r][j + 1];
                    if (j >= n0)
                        flag = 1;
                    for (int k = 1, s = 2; k <= r; k++, s <<= 1)
                    {
                        if (d[k][j / s] == 1 && j % s < s / 2 && bit[k].qry((j / s + 1) * s) - bit[k].qry(j / s * s + s / 2))
                            flag = 0;
                        else if (d[k][j / s] == 0 && j % s >= s / 2 && bit[k].qry(j / s * s + s / 2) - bit[k].qry(j / s * s))
                            flag = 0;
                    }
                    if (flag)
                        ans[n0] += j + 1;
                }
            }
            for (int x = t + 1; x <= _ty; x++)
                bit[r].add(x, a[x] >= l[r][x]);
        }
        int ret = 0;
        for (int i = 1; i <= m; i++)
            ret = ret ^ (i * ans[q[i]]);
        cout << ret << '\n';
    }
    return 0;
}
